package com.yiwei.zxp.view;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.zxp.R;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.util.UI;
import com.yiwei.zxp.util.Util;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by xsooy-pc on 18-5-22.
 */

public class QuickServiceDilaog extends Dialog {
    @BindView(R.id.btn_cancel)
    ImageView btnCancel;
    @BindView(R.id.btn_free)
    ImageView btnFree;
    @BindView(R.id.btn_not_free)
    ImageView btnNotFree;
    @BindView(R.id.edit_money)
    EditText editMoney;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    @BindView(R.id.layout_money)
    FrameLayout layoutMoney;
    private Context context;
    private int select_poi = 0;

    public QuickServiceDilaog(Context context) {
        this(context, R.style.DialogStyle);
    }

    public QuickServiceDilaog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
        initView();
    }

    private void initView() {
        setContentView(R.layout.dialog_quick_service);
        ButterKnife.bind(this);
        setCancelable(false);

        Window mWindow = getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        lp.width = Util.getDisplayMetrics(context).widthPixels;
        lp.gravity = Gravity.BOTTOM;
        mWindow.setAttributes(lp);

        editMoney.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                //删除.后面超过两位的数字
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + 3);
                        editMoney.setText(s);
                        editMoney.setSelection(s.length());
                    }
                }

                //如果.在起始位置,则起始位置自动补0
                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    editMoney.setText(s);
                    editMoney.setSelection(2);
                }

                //如果起始位置为0并且第二位跟的不是".",则无法后续输入
                if (s.toString().startsWith("0")
                        && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        editMoney.setText(s.subSequence(0, 1));
                        editMoney.setSelection(1);
                        return;
                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }

        });
    }

    @OnClick({R.id.btn_cancel, R.id.btn_free, R.id.btn_not_free, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_cancel:
                dismiss();
                break;
            case R.id.btn_free:
                select_poi = 1;
                btnFree.setImageResource(R.mipmap.img_service_select);
                btnNotFree.setImageResource(R.mipmap.img_service_unselect);
                layoutMoney.setVisibility(View.GONE);
                break;
            case R.id.btn_not_free:
                select_poi = 2;
                btnFree.setImageResource(R.mipmap.img_service_unselect);
                btnNotFree.setImageResource(R.mipmap.img_service_select);
                layoutMoney.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_submit:
                if (select_poi == 0) {
                    ToastUtil.makeText(context, "请选择快递费用");
                    return;
                }
                if (select_poi == 2 && TextUtils.isEmpty(UI.toString(editMoney))) {
                    ToastUtil.makeText(context, "请输入快递费用");
                    return;
                }
                if (onChooseListener != null) {
                    onChooseListener.onQuickService(select_poi,UI.toString(editMoney) );
                }
                dismiss();
                break;
        }
    }

    private OnChooseListener onChooseListener;

    public QuickServiceDilaog setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
        return this;
    }

    public interface OnChooseListener {
        void onQuickService(int selectPoi, String money);
    }
}
